
#include "..\Common\Main.h"
#include "..\Common\glaux.h"
#include "..\\Common\Camera1.h"
#include "TexturedBox.h"
#include "..\Common\OGLCamera.h"

using namespace Engine;

App app;

OGLCamera cam;
const float piover180 = 0.0174532925f;
float heading;
float xpos;
float zpos;

GLfloat	yrot;				
GLfloat walkbias = 0;
GLfloat walkbiasangle = 0;
GLfloat lookupdown = 0.0f;
GLfloat	z=0.0f;				

bool mLight = true;
bool mLPressed = false;

TexturedBox texturedBox;

GLfloat LightAmbient[] = {0.5f,0.5f,0.5f,1.0f};
GLfloat LightDiffuse[] = {1.0f,1.0f,1.0f,1.0f};
GLfloat LightPosition[] = {0,2.0f,1.0f};


void InitGL()
{
	
	texturedBox.LoadTextures();

	glEnable(GL_TEXTURE_2D);
	glShadeModel(GL_SMOOTH);
	glClearColor(0.0f,0.0f,0.0f,0.5f);
	glClearDepth(1.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LESS);
	glShadeModel(GL_SMOOTH);							// Enables Smooth Color Shading
	glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);

	glLightfv(GL_LIGHT1, GL_AMBIENT,LightAmbient);
	glLightfv(GL_LIGHT1,GL_DIFFUSE,LightDiffuse);
	glLightfv(GL_LIGHT1,GL_POSITION,LightDiffuse);
	glEnable(GL_LIGHT1);
	glMatrixMode( GL_MODELVIEW );
}



int DrawGLScene()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear The Screen And The Depth Buffer
	
	glLoadIdentity();									// Reset The View
	glTranslatef(0.0f,0.0f,-5.0f);

	//texturedBox.Draw();

	cam.UpdateViewMatrix();

	GLfloat x_m, y_m, z_m, u_m, v_m;
	GLfloat xtrans = -xpos;
	GLfloat ztrans = -zpos;
	GLfloat ytrans = -walkbias-0.25f;
	GLfloat sceneroty = 360.0f - yrot;

	glBindTexture(GL_TEXTURE_2D, texturedBox.mFilter);
	
	texturedBox.Draw();
	return true;
}

LRESULT CALLBACK Engine::WndProc(	HWND	hWnd,UINT	uMsg,WPARAM	wParam,	LPARAM	lParam)			
{
	switch (uMsg)									// Check For Windows Messages
	{
		case WM_CLOSE:								// Did We Receive A Close Message?
		{
			PostQuitMessage(0);						// Send A Quit Message
			return 0;								// Jump Back
		}
		case WM_KEYDOWN:							// Is A Key Being Held Down?
		{
			app.mKeys[wParam] = TRUE;					// If So, Mark It As TRUE
			return 0;								// Jump Back
		}

		case WM_KEYUP:								// Has A Key Been Released?
		{
			app.mKeys[wParam] = FALSE;					// If So, Mark It As FALSE
			
			return 0;								// Jump Back
		}

		case WM_MOUSEMOVE:
			app.mIsMouseMoving = true;
			cam.mIsMouseMoving = true;
			app.mMouseCurPosition.x =  GET_X_LPARAM(lParam); 
			app.mMouseCurPosition.y = GET_Y_LPARAM(lParam);
			return 0;
	}

	// Pass All Unhandled Messages To DefWindowProc
	return DefWindowProc(hWnd,uMsg,wParam,lParam);
}


int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow)
{
	MSG msg;
	BOOL done = FALSE;

	app.Initialize(hInstance);

	Engine::CreateGLWindow(hInstance, app);
	ReSizeGLScene(app.mWindow->mWindowRect->right, app.mWindow->mWindowRect->bottom);
	InitGL();

	while(!done)									// Loop That Runs While done=FALSE
	{
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))	// Is There A Message Waiting?
		{
			if (msg.message==WM_QUIT)				// Have We Received A Quit Message?
			{
				done=TRUE;							// If So done=TRUE
			}
			else									// If Not, Deal With Window Messages
			{
				TranslateMessage(&msg);				// Translate The Message
				DispatchMessage(&msg);				// Dispatch The Message
			}
		}
		else										// If There Are No Messages
		{
			app.UpdateDeltaTime();
			cam.UpdateCameraInput(&app.mMouseCurPosition,app.mKeys, app.mDeltaTime);
			DrawGLScene();
			// Draw The Scene.  Watch For ESC Key And Quit Messages From DrawGLScene()
			if (app.mKeys[VK_ESCAPE])	// Active?  Was There A Quit Received?
			{
				done=TRUE;							// ESC or DrawGLScene Signalled A Quit
			}
			else
			{
				SwapBuffers(app.mWindow->mHdc);

				if(app.mKeys['L'] && !mLPressed)
					{
						mLPressed = TRUE;
						mLight = !mLight;
						if(!mLight)
							glDisable(GL_LIGHTING);
						else glDisable(GL_LIGHTING);
						
					}

				if(!app.mKeys['L'])
						mLPressed = FALSE;

				

			}
		}
	}

	KillGLWindow(app);
	return msg.wParam;						// Exit The Program
}